{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "# 获取环境变量\n",
    "from google.colab import userdata\n",
    "import os\n",
    "ANTHROPIC_API_KEY = userdata.get('ANTHROPIC_API_KEY')\n",
    "os.environ[\"ANTHROPIC_API_KEY\"] = ANTHROPIC_API_KEY"
   ],
   "metadata": {
    "id": "qZcV0AUwOnfm"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "2fmBd_EB-9OS",
    "outputId": "36dbc551-e66f-4262-e967-912bc6a194bf"
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "/content/drive/MyDrive/movies_analysis/movie_reviews.xlsx\n",
      "/content/drive/MyDrive/movies_analysis/movie_description.xlsx\n",
      "/content/drive/MyDrive/movies_analysis/movies_analysis_score.xlsx\n",
      "/content/drive/MyDrive/movies_analysis/my_movie_analysis_score.ipynb\n"
     ]
    }
   ],
   "source": [
    "# 确保文件存在\n",
    "import pandas as pd\n",
    "import os\n",
    "for dirname, _, filenames in os.walk('/content/drive/MyDrive/movies_analysis'):\n",
    "    for filename in filenames:\n",
    "        print(os.path.join(dirname, filename))"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# 看看文件\n",
    "df_raw = pd.read_excel(\"/content/drive/MyDrive/movies_analysis/movie_reviews.xlsx\")\n",
    "df_raw.head()"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "id": "kZgtYUk2Bjpi",
    "outputId": "f12f415f-5f07-45c7-e6b7-5e27e36fab2d"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "   review_id                                             review\n",
       "0          1  ...this is not that.\\n\\nWhile it was certainly...\n",
       "1          2  While I am loathe to criticise a fairly origin...\n",
       "2          3  The camera \"inside\" the gladiator helmet is ju...\n",
       "3          4  Very pleased with this show so far. Seems to p...\n",
       "4          5  If you enjoy the series, do NOT read the books..."
      ],
      "text/html": [
       "\n",
       "  <div id=\"df-fa0b2bd3-9ac7-425b-bb07-1f62ebbd3daf\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>review_id</th>\n",
       "      <th>review</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>...this is not that.\\n\\nWhile it was certainly...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>While I am loathe to criticise a fairly origin...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>The camera \"inside\" the gladiator helmet is ju...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Very pleased with this show so far. Seems to p...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>If you enjoy the series, do NOT read the books...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-fa0b2bd3-9ac7-425b-bb07-1f62ebbd3daf')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-fa0b2bd3-9ac7-425b-bb07-1f62ebbd3daf button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-fa0b2bd3-9ac7-425b-bb07-1f62ebbd3daf');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-9f27d638-3993-46e7-b803-415d795c5da8\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-9f27d638-3993-46e7-b803-415d795c5da8')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-9f27d638-3993-46e7-b803-415d795c5da8 button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "dataframe",
       "variable_name": "df_raw",
       "summary": "{\n  \"name\": \"df_raw\",\n  \"rows\": 50,\n  \"fields\": [\n    {\n      \"column\": \"review_id\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 14,\n        \"min\": 1,\n        \"max\": 50,\n        \"num_unique_values\": 50,\n        \"samples\": [\n          14,\n          40,\n          31\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"review\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 50,\n        \"samples\": [\n          \"I was halfway through the American series before I even KNEW there was a British series. I think a was a little disappointed that we didn't create it, but I was also happy that it was one of the few shows that we copied without ruining it. When I checked some of the user reviews, I was surprised to see how many thought the show to be appalling. OK, not everyone will share my love for the show, but to rate it SO badly? But when I looked, the author of nearly every bad review was a fan of the original show. This simply sounds like a case of liking what you know. \\\"I don't care how good their cooking is. It ain't as good as my Mom's!\\\"\\n\\nOnce I finished the American series, I gave it about a month to settle in and then started watching the British series. But I'd like to think that I was objective enough to judge it on its on merits, and not simply that it's different than what I'm used to.\\n\\nSince, as many have pointed out, the script is nearly word-for-word identical, the difference lies mainly in how the actors portray the characters.\\n\\nSo I'm going to give my character-by-character head-to-head appraisal of UK vs. US. I'm going to use the character names rather than the actors' names for simplicity. Starting from the youngest...\\n\\nLittle Debbie: UK wins this one hands down. No contest. She steals every scene she's in. Who can not fall in love with this girl???\\n\\nCarl: This one's close, but the UK one is (at least in the first season) a little more deranged and fun to watch.\\n\\nIan: Another close one, but this goes to UK, too. US Ian is somber and good looking, but UK Ian always seems a bit panicky, and the wide-angle closeups of his face make him look pretty bizarre.\\n\\nLip: This is solidly with the US. I like US Lip's darkness. He seems more responsible and intelligent. UK Lip is just kind of impish and unsure. You can depend on US Lip and he offers some of the only real family support to Fiona.\\n\\nKevin: Dead heat.\\n\\nVeronica: Very different performances by each, but in the end, I like them the same.\\n\\nFiona: This was a difficult one to call, but I'm giving it to the US. And this is probably because I saw them first. I just like her better. Hard to put my finger on the reason.\\n\\nFrank: Sorry, UK, but I just don't like your Frank. I understand the character is usually drunk, but he seems that way even before he starts drinking. He seems clinically stupid. US Frank (Macy) is equally as obnoxious, entitled, selfish and deluded, but he only seems drunk when he's drunk. At other times, his pontificating is fun to listen to. You feel good about hating the guy, because he's like this by choice. With UK Frank, you have to pity him. I feel like I'm laughing at someone who's mentally challenged.\",\n          \"Quality of the writing has been in decline for years, losing Emmy Rossum was a major blow, and now in the final season there is social justice to tend, to hell with the loyal fans.\\n\\nIn the season opener as Frank is pontificating on the vast contributions by the Gallaghers in building the city of Chicago, predictably we are treated with a lingering shot of a Black Lives Matter mural, easy to see what the theme of this season is going to be. Excuse me but I'm not going to stick around for the Trump jokes, I'm out. Now I'm off to cancel my subscription to Showtime, like I said its been fun.\",\n          \"One of the funniest shows ever. But like someone wrote before this, season 11 and 10 are SOOO BAD. 10 had some emotional moments but besides that no. And 11 is hard to watch. Like I have to force myself to watch the weekly episodes. I'm only watching them to say I've seen all the seasons.\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
      }
     },
     "metadata": {},
     "execution_count": 3
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# 对每个列进行空值检查,强迫症\n",
    "df_raw.isnull().sum()"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FNG9mFOjCoro",
    "outputId": "23943002-86a2-4c70-e365-95d4bb971fe8"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "review_id    0\n",
       "review       0\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# 安装langchain库\n",
    "!pip install langchain-anthropic\n",
    "# 更新一下版本\n",
    "!pip install -qU langchain-anthropic"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "nUAF7EFWOtbb",
    "outputId": "5ee7b601-76aa-4462-f301-06aac7bd0245"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Requirement already satisfied: langchain-anthropic in /usr/local/lib/python3.10/dist-packages (0.1.4)\n",
      "Requirement already satisfied: anthropic<1,>=0.17.0 in /usr/local/lib/python3.10/dist-packages (from langchain-anthropic) (0.21.3)\n",
      "Requirement already satisfied: defusedxml<0.8.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from langchain-anthropic) (0.7.1)\n",
      "Requirement already satisfied: langchain-core<0.2,>=0.1 in /usr/local/lib/python3.10/dist-packages (from langchain-anthropic) (0.1.35)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (3.7.1)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (1.7.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (0.27.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (2.6.4)\n",
      "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (1.3.1)\n",
      "Requirement already satisfied: tokenizers>=0.13.0 in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (0.15.2)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from anthropic<1,>=0.17.0->langchain-anthropic) (4.10.0)\n",
      "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (6.0.1)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (1.33)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (0.1.36)\n",
      "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (23.2)\n",
      "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (2.31.0)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain-anthropic) (8.2.3)\n",
      "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->anthropic<1,>=0.17.0->langchain-anthropic) (3.6)\n",
      "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->anthropic<1,>=0.17.0->langchain-anthropic) (1.2.0)\n",
      "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->anthropic<1,>=0.17.0->langchain-anthropic) (2024.2.2)\n",
      "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->anthropic<1,>=0.17.0->langchain-anthropic) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->anthropic<1,>=0.17.0->langchain-anthropic) (0.14.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2,>=0.1->langchain-anthropic) (2.4)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.0->langchain-core<0.2,>=0.1->langchain-anthropic) (3.10.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->anthropic<1,>=0.17.0->langchain-anthropic) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.16.3 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->anthropic<1,>=0.17.0->langchain-anthropic) (2.16.3)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain-core<0.2,>=0.1->langchain-anthropic) (3.3.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain-core<0.2,>=0.1->langchain-anthropic) (2.0.7)\n",
      "Requirement already satisfied: huggingface_hub<1.0,>=0.16.4 in /usr/local/lib/python3.10/dist-packages (from tokenizers>=0.13.0->anthropic<1,>=0.17.0->langchain-anthropic) (0.20.3)\n",
      "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.0->anthropic<1,>=0.17.0->langchain-anthropic) (3.13.3)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.0->anthropic<1,>=0.17.0->langchain-anthropic) (2023.6.0)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.0->anthropic<1,>=0.17.0->langchain-anthropic) (4.66.2)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# 引入模型\n",
    "from langchain_anthropic import AnthropicLLM\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "# warn忽略,问就是老模型就是这个版本\n",
    "model = AnthropicLLM(model='claude-2.1')\n",
    "\n",
    "# 测试的电影评论\n",
    "this_review = \"\"\"While I am loathe to criticise a fairly original story and something quite different from the norm, this film was definitely not for me. I'm a big fan of Stone, Ruffalo and Defoe so it pains me even more to be negative but I did not like this at all and feel tricked by the high review scores, to the point where I feel the need to leave a low score to balance it out a bit.\n",
    "\n",
    "I was left thinking suddenly I no longer understand film for this to have scored so high.\n",
    "\n",
    "I'm not sure why this is deemed a comedy, it is much more a horror in the conventional sense. Frequently grotesque and music designed to make you want to hit the mute button. I tried to like the style and cinematography but I just found the whole story so unpleasant as to be completely distracting.\n",
    "\n",
    "The one thing I did find funny was the first frame saying 'CONTAINS TOBACCO REFERENCES', which is then proceeded by the opening shot of a suicide. What a bizarre world we are living in.\"\"\"\n",
    "# 随手写一个prompt模板\n",
    "prompt = PromptTemplate.from_template(\"\"\"Classify the sentiment of the movie review into 10 levels, ranging from 1 to 10, with 10 being the most positive and 1 being the most negative. Rate the current movie based on the review:\n",
    "{review}\n",
    "\n",
    "just return the score with nothing else\"\"\")"
   ],
   "metadata": {
    "id": "5pFi6F-xDDtF",
    "outputId": "6ef16e80-eece-4e36-8bb8-7c6f44eba13b",
    "colab": {
     "base_uri": "https://localhost:8080/"
    }
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/langchain_anthropic/llms.py:176: UserWarning: This Anthropic LLM is deprecated. Please use `from langchain_community.chat_models import ChatAnthropic` instead\n",
      "  warnings.warn(\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "# 定义一个chain\n",
    "my_chain =  prompt | model | StrOutputParser()\n",
    "# 测试执行\n",
    "my_chain.invoke({\"review\": this_review})"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "AvfVDu9NQFbY",
    "outputId": "ea897b7e-8858-45a2-879d-1cc0be26bfff"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'\\n3'"
      ],
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      }
     },
     "metadata": {},
     "execution_count": 7
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_anthropic import ChatAnthropic\n",
    "# 写个函数\n",
    "def getScore(review):\n",
    "  # 换个版本.tmd\n",
    "  llm = ChatAnthropic(temperature=0,model_name=\"claude-3-opus-20240229\")\n",
    "  # prompt模板\n",
    "  prompt = PromptTemplate.from_template(\"\"\"Classify the sentiment of the movie review into 10 levels, ranging from 1 to 10, with 10 being the most positive and 1 being the most negative. Rate the current movie based on the review:\n",
    "  {review}\n",
    "\n",
    "  just return the score with nothing else\"\"\")\n",
    "  # 定义一个chain\n",
    "  my_chain =  prompt | llm | StrOutputParser()\n",
    "  return my_chain.invoke({\"review\": review})"
   ],
   "metadata": {
    "id": "OkhaplYrShvY"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "# 逐行读取review列\n",
    "for index, row in df_raw.iterrows():\n",
    "    review = row['review']\n",
    "    review_id = str(row['review_id'])\n",
    "    print(review+\"\\n\\n\\n\")\n",
    "    score = getScore(review)\n",
    "    cleaned_score = score.replace(\"\\n\", \"\")\n",
    "    print(\"###########\"+review_id+\"###########\"+review+\"###########\"+cleaned_score+\"###########\")\n",
    "    break"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "kC54Sa_hRy8_",
    "outputId": "4ac64f4d-ed30-4d66-83af-8e6be401825c"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "...this is not that.\n",
      "\n",
      "While it was certainly groundbreaking and a huge hit at the time, in retrospect great parts of it was rather cheasy and cheapish.\n",
      "\n",
      "The 2024 version feels a great deal more modern and budgeted bigger, it's rougher, it's dirtier, the action scenes are crafted much better, the camera work is better, and so far the script seems better than the original series, with better dialogue allowing the characters more complexity and debth, which is especially important to American and European audiences, when depicting a very \"foreign\" culture.\n",
      "\n",
      "I should mention this is based off the first 2 episodes, so we are yet to see large scale battle scenes, but looking at the teaser clips, they look exellent, so I am hopefull.\n",
      "\n",
      "It's a show that - even after 2 episodes - makes you annoyed it is a limited series, by nature of James Clavell having written only one novel on feudal Japan.. but I actually predict this will be such a big hit, that FX will attempt to have scripts written for either a prequal, continuation - or spin offs - because that period of Japanese history is ripe with potential - and come on, who doesn't like samurai's?\n",
      "\n",
      "This is one of the \"must see\" shows of the year, where you will feel left out if you miss it.\n",
      "\n",
      "Enjoy :)\n",
      "\n",
      "\n",
      "\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/langchain_anthropic/llms.py:176: UserWarning: This Anthropic LLM is deprecated. Please use `from langchain_community.chat_models import ChatAnthropic` instead\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "###########1###########...this is not that.\n",
      "\n",
      "While it was certainly groundbreaking and a huge hit at the time, in retrospect great parts of it was rather cheasy and cheapish.\n",
      "\n",
      "The 2024 version feels a great deal more modern and budgeted bigger, it's rougher, it's dirtier, the action scenes are crafted much better, the camera work is better, and so far the script seems better than the original series, with better dialogue allowing the characters more complexity and debth, which is especially important to American and European audiences, when depicting a very \"foreign\" culture.\n",
      "\n",
      "I should mention this is based off the first 2 episodes, so we are yet to see large scale battle scenes, but looking at the teaser clips, they look exellent, so I am hopefull.\n",
      "\n",
      "It's a show that - even after 2 episodes - makes you annoyed it is a limited series, by nature of James Clavell having written only one novel on feudal Japan.. but I actually predict this will be such a big hit, that FX will attempt to have scripts written for either a prequal, continuation - or spin offs - because that period of Japanese history is ripe with potential - and come on, who doesn't like samurai's?\n",
      "\n",
      "This is one of the \"must see\" shows of the year, where you will feel left out if you miss it.\n",
      "\n",
      "Enjoy :)###########8###########\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# 测试输出\n",
    "output_file_path = \"/content/drive/MyDrive/movies_analysis/movies_analysis_score.xlsx\"\n",
    "\n",
    "data = {\n",
    "    'review_id': [1, 2, 3, 4, 5],\n",
    "    'review': ['Good product', 'Bad experience', 'Great service', 'Average', 'Excellent'],\n",
    "    'sentiment': [4.5, 2.0, 5.0, 3.0, 5.0]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "df.to_excel(output_file_path, index=False)\n",
    "# 看看文件\n",
    "df_raw2 = pd.read_excel(output_file_path)\n",
    "df_raw2.head()"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "id": "bjfGs9GIUn0l",
    "outputId": "833a9b03-771b-4c84-a96a-0fee032312d4"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "   review_id          review  sentiment\n",
       "0          1    Good product        4.5\n",
       "1          2  Bad experience        2.0\n",
       "2          3   Great service        5.0\n",
       "3          4         Average        3.0\n",
       "4          5       Excellent        5.0"
      ],
      "text/html": [
       "\n",
       "  <div id=\"df-b9ded42a-8f02-4e22-afa9-028eb35dac43\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>review_id</th>\n",
       "      <th>review</th>\n",
       "      <th>sentiment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Good product</td>\n",
       "      <td>4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Bad experience</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Great service</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Average</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b9ded42a-8f02-4e22-afa9-028eb35dac43')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-b9ded42a-8f02-4e22-afa9-028eb35dac43 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-b9ded42a-8f02-4e22-afa9-028eb35dac43');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-f31b5f6e-32e6-4ef7-96f4-99c58e77f66c\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-f31b5f6e-32e6-4ef7-96f4-99c58e77f66c')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-f31b5f6e-32e6-4ef7-96f4-99c58e77f66c button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "dataframe",
       "variable_name": "df_raw2",
       "summary": "{\n  \"name\": \"df_raw2\",\n  \"rows\": 5,\n  \"fields\": [\n    {\n      \"column\": \"review_id\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 1,\n        \"min\": 1,\n        \"max\": 5,\n        \"num_unique_values\": 5,\n        \"samples\": [\n          2,\n          5,\n          3\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"review\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 5,\n        \"samples\": [\n          \"Bad experience\",\n          \"Excellent\",\n          \"Great service\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"sentiment\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 1.3416407864998738,\n        \"min\": 2.0,\n        \"max\": 5.0,\n        \"num_unique_values\": 4,\n        \"samples\": [\n          2.0,\n          3.0,\n          4.5\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
      }
     },
     "metadata": {},
     "execution_count": 32
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "import time\n",
    "# 数据产出\n",
    "review_ids=[]\n",
    "reviews=[]\n",
    "sentiments=[]\n",
    "for index, row in df_raw.iterrows():\n",
    "    review = row['review']\n",
    "    review_id = str(row['review_id'])\n",
    "    score = getScore(review)\n",
    "    # 执行快了,说什么超过限额\n",
    "    time.sleep(0.5)\n",
    "    cleaned_score = score.replace(\"\\n\", \"\")\n",
    "    print(\"###########\"+review_id+\"###########\"+cleaned_score+\"###########\")\n",
    "    review_ids.append(review_id)\n",
    "    reviews.append(review)\n",
    "    sentiments.append(cleaned_score)\n",
    "\n",
    "\n",
    "output_file_path = \"/content/drive/MyDrive/movies_analysis/movies_analysis_score.xlsx\"\n",
    "\n",
    "data = {\n",
    "    'review_id': review_ids,\n",
    "    'review': reviews,\n",
    "    'sentiment': sentiments\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "df.to_excel(output_file_path, index=False)\n",
    "# 看看文件\n",
    "df_raw2 = pd.read_excel(output_file_path)\n",
    "df_raw2.head()"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "EJFkJ91RVlZ7",
    "outputId": "a6b79582-db0f-4ddf-d6ec-5e58d294bfe3"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "###########1###########8###########\n",
      "###########2###########3###########\n",
      "###########3###########2###########\n",
      "###########4###########9###########\n",
      "###########5###########3###########\n",
      "###########6###########8###########\n",
      "###########7###########3###########\n",
      "###########8###########9###########\n",
      "###########9###########9###########\n",
      "###########10###########9###########\n",
      "###########11###########9###########\n",
      "###########12###########3###########\n",
      "###########13###########9###########\n",
      "###########14###########7###########\n",
      "###########15###########2###########\n",
      "###########16###########10###########\n",
      "###########17###########10###########\n",
      "###########18###########8###########\n",
      "###########19###########9###########\n",
      "###########20###########4###########\n",
      "###########21###########2###########\n",
      "###########22###########4###########\n",
      "###########23###########9###########\n",
      "###########24###########9###########\n",
      "###########25###########7###########\n",
      "###########26###########1###########\n",
      "###########27###########10###########\n",
      "###########28###########10###########\n",
      "###########29###########10###########\n",
      "###########30###########8###########\n",
      "###########31###########4###########\n",
      "###########32###########2###########\n",
      "###########33###########9###########\n",
      "###########34###########5###########\n",
      "###########35###########9###########\n",
      "###########36###########10###########\n",
      "###########37###########1###########\n",
      "###########38###########2###########\n",
      "###########39###########9###########\n",
      "###########40###########2###########\n",
      "###########41###########10###########\n",
      "###########42###########2###########\n",
      "###########43###########1###########\n",
      "###########44###########6###########\n",
      "###########45###########10###########\n",
      "###########46###########2###########\n",
      "###########47###########3###########\n",
      "###########48###########3###########\n",
      "###########49###########3###########\n",
      "###########50###########8###########\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "   review_id                                             review  sentiment\n",
       "0          1  ...this is not that.\\n\\nWhile it was certainly...          8\n",
       "1          2  While I am loathe to criticise a fairly origin...          3\n",
       "2          3  The camera \"inside\" the gladiator helmet is ju...          2\n",
       "3          4  Very pleased with this show so far. Seems to p...          9\n",
       "4          5  If you enjoy the series, do NOT read the books...          3"
      ],
      "text/html": [
       "\n",
       "  <div id=\"df-6c0d9def-61ff-443a-a7a4-385de9884a5f\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>review_id</th>\n",
       "      <th>review</th>\n",
       "      <th>sentiment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>...this is not that.\\n\\nWhile it was certainly...</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>While I am loathe to criticise a fairly origin...</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>The camera \"inside\" the gladiator helmet is ju...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Very pleased with this show so far. Seems to p...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>If you enjoy the series, do NOT read the books...</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-6c0d9def-61ff-443a-a7a4-385de9884a5f')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-6c0d9def-61ff-443a-a7a4-385de9884a5f button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-6c0d9def-61ff-443a-a7a4-385de9884a5f');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-f609ec93-69ef-4264-82cc-eca69cc1079d\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-f609ec93-69ef-4264-82cc-eca69cc1079d')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-f609ec93-69ef-4264-82cc-eca69cc1079d button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "dataframe",
       "variable_name": "df_raw2",
       "summary": "{\n  \"name\": \"df_raw2\",\n  \"rows\": 50,\n  \"fields\": [\n    {\n      \"column\": \"review_id\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 14,\n        \"min\": 1,\n        \"max\": 50,\n        \"num_unique_values\": 50,\n        \"samples\": [\n          14,\n          40,\n          31\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"review\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 50,\n        \"samples\": [\n          \"I was halfway through the American series before I even KNEW there was a British series. I think a was a little disappointed that we didn't create it, but I was also happy that it was one of the few shows that we copied without ruining it. When I checked some of the user reviews, I was surprised to see how many thought the show to be appalling. OK, not everyone will share my love for the show, but to rate it SO badly? But when I looked, the author of nearly every bad review was a fan of the original show. This simply sounds like a case of liking what you know. \\\"I don't care how good their cooking is. It ain't as good as my Mom's!\\\"\\n\\nOnce I finished the American series, I gave it about a month to settle in and then started watching the British series. But I'd like to think that I was objective enough to judge it on its on merits, and not simply that it's different than what I'm used to.\\n\\nSince, as many have pointed out, the script is nearly word-for-word identical, the difference lies mainly in how the actors portray the characters.\\n\\nSo I'm going to give my character-by-character head-to-head appraisal of UK vs. US. I'm going to use the character names rather than the actors' names for simplicity. Starting from the youngest...\\n\\nLittle Debbie: UK wins this one hands down. No contest. She steals every scene she's in. Who can not fall in love with this girl???\\n\\nCarl: This one's close, but the UK one is (at least in the first season) a little more deranged and fun to watch.\\n\\nIan: Another close one, but this goes to UK, too. US Ian is somber and good looking, but UK Ian always seems a bit panicky, and the wide-angle closeups of his face make him look pretty bizarre.\\n\\nLip: This is solidly with the US. I like US Lip's darkness. He seems more responsible and intelligent. UK Lip is just kind of impish and unsure. You can depend on US Lip and he offers some of the only real family support to Fiona.\\n\\nKevin: Dead heat.\\n\\nVeronica: Very different performances by each, but in the end, I like them the same.\\n\\nFiona: This was a difficult one to call, but I'm giving it to the US. And this is probably because I saw them first. I just like her better. Hard to put my finger on the reason.\\n\\nFrank: Sorry, UK, but I just don't like your Frank. I understand the character is usually drunk, but he seems that way even before he starts drinking. He seems clinically stupid. US Frank (Macy) is equally as obnoxious, entitled, selfish and deluded, but he only seems drunk when he's drunk. At other times, his pontificating is fun to listen to. You feel good about hating the guy, because he's like this by choice. With UK Frank, you have to pity him. I feel like I'm laughing at someone who's mentally challenged.\",\n          \"Quality of the writing has been in decline for years, losing Emmy Rossum was a major blow, and now in the final season there is social justice to tend, to hell with the loyal fans.\\n\\nIn the season opener as Frank is pontificating on the vast contributions by the Gallaghers in building the city of Chicago, predictably we are treated with a lingering shot of a Black Lives Matter mural, easy to see what the theme of this season is going to be. Excuse me but I'm not going to stick around for the Trump jokes, I'm out. Now I'm off to cancel my subscription to Showtime, like I said its been fun.\",\n          \"One of the funniest shows ever. But like someone wrote before this, season 11 and 10 are SOOO BAD. 10 had some emotional moments but besides that no. And 11 is hard to watch. Like I have to force myself to watch the weekly episodes. I'm only watching them to say I've seen all the seasons.\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"sentiment\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 3,\n        \"min\": 1,\n        \"max\": 10,\n        \"num_unique_values\": 10,\n        \"samples\": [\n          5,\n          3,\n          10\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"
      }
     },
     "metadata": {},
     "execution_count": 19
    }
   ]
  }
 ]
}
